const { ccclass, property } = cc._decorator;
import n from "./MatchGameData"
import l from "./MatchGameAudioMgr"
import s, { AnimType } from "./BasePageCtrl"
import c from "./AudioManager"
import u from "./EventMgr"
import p from "./MatchGameEventType"
import d from "./AdManager"
import h from "./SdkHelper"
import f from "./EngineUtil"
import g from "./AbTestMgr"
import y from "./PeripheralSystemDataMgr"
import m from "./MatchGameSystemMgr"
import _ from "./PeripheralSystemMgr"
import v from "./UiManage"
import b from "./MatchGameSuccessSettlementPage"

@ccclass
export default class MatchGameSuccessSettlementPageCtrl {
@property(cc.Node)
ui = null;

canClick = false;
step = 0;

onLoad() {
    this.onUILoad();
    this.ui.getComponent(s)?.setAnimType(AnimType.SCALE);
    this.ui.getComponent(s)?.setTouchControl(false);
    this.ui.getComponent(s)?.setHasPeneLock(true);
    this.ui.getComponent(s)?.setHasBlack(true);
    this.ui.getComponent(s)?.setHasTouchLock(false);
    this.ui.getComponent(s)?.setHasBlackTouch(false);
    this.addButtonListen();
};

start() {};

onUILoad() {
    this.ui.addComponent(b);
};

_init(e?) {
    c.getInstance().pauseMusic("bgm", true);
    this.canClick = false;
    this.node.setContentSize(cc.winSize);
    var a = e.strtFlyNode, o = e.step;
    this.step = o;
    this.ui.goldCoinFlySpine.active = false;
    this.ui.hb_sp.active = false;
    this.ui.jj_sp.active = false;
    var i = y.isHitOppoReviewBan();
    this.ui.lab_jj_count.active = !i;
    this.ui.spr_bt.active = !i;
    if (i) {
        this.ui.spr_hb.x = -60;
    }
    this.playFlyAni(a);
    this.step, n.getFlyBoxCash();
    this.ui.lab_jj_count.getComponent(cc.RichText).string = "0";
    this.ui.lab_hb_count.getComponent(cc.RichText).string = "" + (this.step / 100).toFixed(2);
    m.matchGameSuccess(this.step,  (e) => {
        var a = "0";
        if (e && e.gold) {
            a = f.getCashBalance(e.gold);
        }
        h.reportData("matchgame_finish_game_success", {
            level: n.gameLevel,
            game_time: n.getGameTime()
        });
        t.ui.label_bt_num.getComponent(cc.Label).string = a + "y";
    });
    y.setRollingNoticeParent(this.ui.noticeParent);
    u.trigger(p.HIDE_MATCHGAME_NODE);
};

playFlyAni(e) {
    var a = y.isHitOppoReviewBan();
    this.ui.goldCoinFlySpine.active = true;
    this.ui.goldCoinFlySpine.position = cc.v3(f.getNodeAToNodeBPoint(e, this.node));
    var o = this.ui.goldCoinFlySpine.getComponent(sp.Skeleton);
    l.playSuccessPageShowAudio();
    o.setAnimation(0, "idle_2", false);
    o.setAnimation(1, "idle_3", false);
    var i = a ? 0 : this.ui.hb_sp.x;
    cc.tween(o.node).delay(.5).to(.6, {
        x: i,
        y: this.ui.hb_sp.y
    }, {
        easing: "backOut"
    }).call(function () {
        o.node.active = false;
        t.ui.hb_sp.active = true;
        t.ui.content.active = true;
        t.ui.hb_sp.x = a ? 0 : t.ui.hb_sp.x;
        t.canClick = true;
    }).start();
    this.ui.jj_sp.x = -1e3;
    this.ui.jj_sp.active = !a;
    cc.tween(this.ui.jj_sp).delay(.5).to(.6, {
        x: -this.ui.hb_sp.x,
        y: this.ui.hb_sp.y
    }, {
        easing: "backOut"
    }).call(function () { }).start();
};

addButtonListen() {
   UiManager.addButtonListen(this.ui.btn_video, this.doubleCallback, this);
   UiManager.addButtonListen(this.ui.btn_common, this.normalCallback, this);
};

doubleCallback() {
    if (this.canClick) {
        h.reportData("matchgame_click_double_reward", {
            level: n.gameLevel,
            game_time: n.getGameTime()
        });
        y.reportEncourageVideo();
        d.getInstance().playNormalVideoAd(() => {
            var t = y.isArriveNewCity();
            m.successDouble(this.step, t, () => {
                this.hide();
                u.trigger(p.END_MATCH_GAME, {
                    is_tg: 1
                });
                h.reportData("matchgame_get_double_reward", {
                    level: n.gameLevel,
                    game_time: n.getGameTime()
                });
            });
        }, () => { });
    }
};

normalCallback() {
    if (this.canClick) {
        h.reportData("matchgame_click_normal_reward", {
            level: n.gameLevel,
            game_time: n.getGameTime()
        });
        var t = n.force_video, a = g.isBtljFlag();
        y.isHitOppoReviewBan() && y.setRewardGold(0);
        if (t) {
            y.reportEncourageVideo();
            h.showForceToast("看完广告就能获得大量红包");
            d.getInstance().playNormalVideoAd(() => {
                y.reportVideoBuriedPoint(VideoBuriedPoint.FORCE_ENCOURAGE);
                var t = y.isArriveNewCity();
                m.successDouble(this.step, t, () => {
                    this.hide();
                    u.trigger(p.END_MATCH_GAME, {
                        is_tg: 1
                    });
                });
            }, () => {
                a || y.setRewardGold(0);
                this.hide();
                u.trigger(p.END_MATCH_GAME, {
                    is_tg: 1
                });
            });
        } else {
            a || y.setRewardGold(0);
            this.hide();
            u.trigger(p.END_MATCH_GAME, {
                is_tg: 1
            });
        }
    }
};

clickClose() {
    this.hide();
};

_onHide() {
    public _onHide.call(this);
};
}